package Tree;

import java.util.ArrayList;
import java.util.List;

public class 二叉树的所有路径 {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> result = new ArrayList<>();
        preorderTraversal(root, "", result);
        return result;
    }

    private void preorderTraversal(TreeNode root, String s, List<String> result) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            result.add(new String(new StringBuilder(s).append(root.val)));
            return;
        }
        String record = new String(new StringBuilder(s).append(root.val).append("->"));
        preorderTraversal(root.left, record, result);
        preorderTraversal(root.right, record, result);
    }
}
